/**
 * Created by 功振 on 2016/2/20.
 */
angular.module("Portal").directive("ngUniquephone", ["userInfoService", function (userInfoService) {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModel) {
            element.bind('blur', function (e) {
                if (!ngModel || !element.val()) return;
                var currentValue = element.val();
                console.log("currentValue:" + currentValue);
                userInfoService.isPhoneExisted(currentValue).then(function (response) {
                   /* var isExisted = response.data;
                    ngModel.$setValidity('uniquephone', !isExisted);*/
                    var isExisted = response.data.length === 0?false:true;
                    ngModel.$setValidity('uniquephone', !isExisted);
                    console.log("phone number " + currentValue + " existed:" + isExisted);
                }, function (response) {
                    console.log("Checking phone number " + currentValue + " exist failed");
                });

            });
        }
    }
}]);